package capricornus.components.hbox
{
	import capricornus.components.event.ComponentsEvent;
	import capricornus.components.sprite.AbstractComponent;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	
	public class HBox extends AbstractComponent
	{
		private var _horizontalGap:int = 0;
		private var _promptResize:Boolean = false;
		private var _autoRefresh:Boolean = true;
		
		public function HBox()
		{
			super();
			init();
		}
		
		private function init():void
		{
		}
		
		public function set promptResize( value:Boolean ):void
		{
			_promptResize = value;
		}
		
		public function set autoRefresh( value:Boolean ):void
		{
			_autoRefresh = value;
		}
		
		public function set horizontalGap( value:int ):void
		{
			_horizontalGap = value;
			if( _autoRefresh )
			{
				refresh();
			}
		}
		
		public function get length():int
		{
			return _container.numChildren;
		}
		
		public function addItem( item:DisplayObject ):void
		{
			_container.addChild( item );
			if( _autoRefresh )
			{
				refresh();
			}
		}
		
		public function addItemAt( item:DisplayObject, index:int ):void
		{
			_container.addChildAt( item, index );
			if( _autoRefresh ) refresh();
		}
		
		public function removeItem( item:DisplayObject ):void
		{
			_container.removeChild( item );
			if( _autoRefresh )
			{
				refresh();
			}
		}
		
		public function getItemAt( index:int ):DisplayObject
		{
			return _container.getChildAt( index );
		}
		
		public function removeItemAt( index:int ):DisplayObject
		{
			var ret:DisplayObject = _container.removeChildAt( index );
			if( _autoRefresh )
			{
				refresh();
			}
			return ret;
		}
		
		public function getItemIndex( item:DisplayObject ):int
		{
			return _container.getChildIndex( item );
		}
		
		public function refresh():void
		{
			var len:int = _container.numChildren;
			var x:int = 0;
			for( var i:int=0; i<len; i++ )
			{
				var dis:DisplayObject = _container.getChildAt( i );
				dis.x = x;
				x = x + dis.width + _horizontalGap;
			}
			if( _promptResize ) dispatchEvent( new ComponentsEvent( ComponentsEvent.RESIZE ) );
		}
	}
}